﻿using UnityEngine;
using System.Collections;
using System;

/// 玉米广告
public class YumiAds : AdsBase
{
	static AndroidJavaObject jc = null;

	public override void Init (params string[] key)
	{
		TJLog ("Lestore Init");

		if (jc == null) {
			jc = new AndroidJavaObject ("com.yumi.Main");
		}

		jc.CallStatic ("init", key [0]);
		jc.CallStatic ("setCallBack", "TJSDK", "OnADYumiCallBack");
		jc.CallStatic ("initBanner", 0);
		jc.CallStatic ("initInsert");
		jc.CallStatic ("initVideo");

		LoadInsert ();
	}

	#region  展示banner 视屏 插屏 原生

	public override void ShowBanner (Action<bool> ac)
	{
		bannerCallBack = ac;
		jc.CallStatic ("showBanner");
	}

	public override void CloseBanner ()
	{
		jc.CallStatic ("closeBanner");
	}

	public override void ShowIncent (Action<bool> ac)
	{
		TJLog ("调用播放激励视频广告");
		incentCallBack = ac;
		jc.CallStatic ("showVideo");
	}

	public override void ShowInsertVideo (Action<bool> ac)
	{
		Debug.LogError ("无插屏视频");
	}

	public override void ShowInsert (Action<bool> ac)
	{
		TJLog ("调用播放插屏广告");
		insertCallBack = ac;
		jc.CallStatic ("showInsert");
	}

	#endregion

	public override void OnADCallback (string str)
	{
		TJLog ("广告回调: " + str);

		string[] datas = str.Split ('|');
		switch (datas [0]) {
		case "Banner":
			switch (datas [1]) {
			case "onADReceive":
				TJLog ("接收到Banner广告");
				break;
			case "onNoAD":
				TJLog ("请求Banner广告失败:" + datas [2]);
				if (bannerCallBack != null) {
					bannerCallBack (false);
					bannerCallBack = null;
				}
				break;
			case "onADExposure":
				TJLog ("展示Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (true);
					bannerCallBack = null;
				}
				break;
			case "onADClose":
				TJLog ("关闭Banner广告");
				break;
			case "onADClick":
				TJLog ("点击Banner广告");
				break;
			default:
				break;
			}
			break;
		case "Insert":
			switch (datas [1]) {
			case "onADReceive":
				TJLog ("接收到插屏广告");
				break;
			case "onNoAD":
				TJLog ("请求插屏广告失败:" + datas [2]);
				if (insertCallBack != null) {
					insertCallBack (false);
					insertCallBack = null;
				}
				break;
			case "onADExposure":
				TJLog ("展示插屏广告");
				if (insertCallBack != null) {
					insertCallBack (true);
					insertCallBack = null;
				}
				break;
			case "onADClose":
				TJLog ("关闭插屏广告");
				break;
			case "onADClick":
				TJLog ("点击插屏广告");
				break;
			default:
				break;
			}
			break;
		case "Video":
			switch (datas [1]) {
			case "onADReceive":
				TJLog ("接收到视频广告");
				break;
			case "onNoAD":
				TJLog ("请求视频广告失败:" + datas [2]);
				if (incentCallBack != null) {
					incentCallBack (false);
					incentCallBack = null;
				}
				break;
			case "onADExposure":
				TJLog ("展示视频广告");
				break;
			case "onADClose":
				TJLog ("关闭视频广告");
				break;
			case "onADClick":
				TJLog ("点击视频广告");
				break;
			case "onADIncentived":
				TJLog ("获得激励奖品");
				if (incentCallBack != null) {
					incentCallBack (true);
					incentCallBack = null;
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	#region 判断视频和插屏是否加载好

	public override bool IsBannerReady ()
	{	
		TJLog ("检测Banner状态");
		TJLog ("Banner加载好啦");
		return true;
	}

	public override bool IsInsertReady ()
	{
		TJLog ("检测插屏状态");
		TJLog ("插屏加载好啦");
		return true;
	}

	public override bool IsIncentReady ()
	{
		TJLog ("检测视频状态");
		if (jc.CallStatic<bool> ("isVideoReady")) {
			TJLog ("视频加载好啦");
			return true;
		} else {
			TJLog ("视频没有加载好");
			return false;
		}
	}

	#endregion
	
}
